package com.ls.lserp.modules.setting.web;

import com.ls.lserp.common.persistence.AjaxDone;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.setting.entity.CheckQualityEntity;
import com.ls.lserp.modules.setting.entity.OperatingEntity;
import com.ls.lserp.modules.setting.service.CheckQualityService;
import com.ls.lserp.modules.sys.utils.QualityUtils;
import com.ls.lserp.modules.sys.utils.SystemMessageUtils;

import freemarker.template.Configuration;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.List;

/**
 * @ClassName: CheckProjectController
 * @Description: 订单导出项目数量控制
 * 
 * @author 于辉
 * @date 2017年5月27日
 *
 */

@Controller
@RequestMapping(value = "${adminPath}/check/quality")
public class CheckQualityController extends BaseController{
	
	@Autowired
	// 保存 编辑 删除 方法
	private CheckQualityService checkQualityService;
    @Autowired
    private Configuration freemarkerConfiguration;
    @ModelAttribute
    public CheckQualityEntity get(@RequestParam(required = false) String id) {

        // 判断id是否为空
        if (StringUtils.isNotBlank(id)) {

            // 执行查询语句 查询基本信息
            return checkQualityService.get(Long.parseLong(id));
        } else {

            // 若为空，实例化
            return new CheckQualityEntity();
        }
    }
	
	/**
	 * 进入一览页面
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:view")
	@RequestMapping(value = {""})
	public String list(CheckQualityEntity checkQualityEntity, Model model) {
		if (checkQualityEntity.getOperatingId() == null || checkQualityEntity.getOperatingId() == "") {
			checkQualityEntity.setOperatingId("34");
		}
		checkQualityEntity = checkQualityService.findQualityItemList(checkQualityEntity);
        model.addAttribute ("checkQualityEntity",checkQualityEntity);
		// 返回页面地址
	    return "modules/setting/checkQualityList";
	}
	
	/**
	 * 进入一览页面
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:view")
	@RequestMapping(value = {"newList"})
	public String newList(CheckQualityEntity checkQualityEntity, Model model,HttpSession session) {
		List<OperatingEntity> operatingList = checkQualityService.findOperatingList();
		List<CheckQualityEntity> newItemList = (List<CheckQualityEntity>)session.getAttribute("qcItemList");
		checkQualityEntity.setQcList(newItemList);
		checkQualityEntity.setOperatingList(operatingList);
        model.addAttribute ("checkQualityEntity",checkQualityEntity);
		// 返回页面地址
	    return "modules/setting/checkQualityList";
	}
	
	/**
	 * 进入一览页面
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:view")
	@RequestMapping(value = {"sessionList"})
	public String sessionList(CheckQualityEntity checkQualityEntity, Model model,HttpSession session,HttpServletResponse response) {
		session.setAttribute("qcItemList", checkQualityEntity.getQcList());
		// 返回页面地址
	    return renderString(response,"");
	}
	
	/**
	 * 保存数据
	 * @param checkQualityEntity
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequiresPermissions("check:quality:edit")
	@RequestMapping(value = {"saveForm"})
	public String saveForm(CheckQualityEntity checkQualityEntity, Model model,HttpServletRequest request,HttpServletResponse response){
		try{
			checkQualityService.insertQcId (checkQualityEntity);
			// 清除缓存
			QualityUtils.removeQualityCache();
        } catch (Exception e) {
     	   logger.error(e.getMessage(),e);
             // 提示错误信息
            return renderString(response,AjaxDone.error(SystemMessageUtils.getMessageName("System_error_please_contact_your_administrator")));
        }
		// 返回页面地址
	    return renderString(response, AjaxDone.error(SystemMessageUtils.getMessageName("audit_ok_msg")));
	}
	
	/**
	 * 选择添加质检项目
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:view")
	@RequestMapping(value = {"selectQualityItem"})
	public String selectQualityItem(CheckQualityEntity checkQualityEntity, Model model) {
		List<CheckQualityEntity> itemList = checkQualityService.findAllQualityItem(checkQualityEntity);
		model.addAttribute("itemList", itemList);
		// 返回页面地址
	    return "modules/setting/qualityItemList";
	}
	
	/**
	 * 删除标准质检项目数据
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:edit")
	@RequestMapping(value = {"deleteItem"})
	public String deleteItem(CheckQualityEntity checkQualityEntity, Model model,HttpServletResponse response) {
		checkQualityService.deleteItem(checkQualityEntity);
		// 返回页面地址
	    return renderString(response, "");
	}
	
	/**
	 * 选择添加质检项目
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:edit")
	@RequestMapping(value = {"form"})
	public String form(CheckQualityEntity checkQualityEntity, Model model) {
		if (checkQualityEntity.getQcId() != null && !"".equals(checkQualityEntity.getQcId())) {
			checkQualityEntity = checkQualityService.get(checkQualityEntity);
			model.addAttribute("checkQualityEntity", checkQualityEntity);
		}
		// 返回页面地址
	    return "modules/setting/qualityItemCreate";
	}
	
	/**
     * 进入check页面
     *
     * @param itemCode
     * @return
     */
    @RequiresPermissions("check:quality:edit")
    @ResponseBody
    @RequestMapping(value = "checkExist")
    public String checkExist(CheckQualityEntity checkQualityEntity,String oldQcItemValue,String oldQcItemCd) {
        try {
        	if ((oldQcItemCd == null || "".equals(oldQcItemCd)) && (oldQcItemValue == null || "".equals(oldQcItemValue))) {
        		if (checkQualityEntity.getQcItemCd() != null && checkQualityService.getItemByCd (checkQualityEntity) == null) {
        			// 返回true
        			return "true";
        		}
			}else{
				if ( !checkQualityEntity.getQcItemCd().equals(oldQcItemCd) || !checkQualityEntity.getQcItemValue().equals(oldQcItemValue)) {
					if (checkQualityEntity.getQcItemCd() != null && checkQualityService.getItemByCd (checkQualityEntity) == null) {
	        			// 返回true
	        			return "true";
	        		}
				}else {
					return "true";
				}
			}
            // 数据值重名
            return "false";

        } catch (Exception e) {
            // 记录日志
            logger.error (e.getMessage (), e);
        }
        // 数据值重名
        return "false";
    }
	
	/**
	 * 选择添加质检项目
	 * @param checkProjectEntity
	 * @param model
	 * @return
	 */
	@RequiresPermissions("check:quality:edit")
	@RequestMapping(value = {"saveData"})
	public String saveData(CheckQualityEntity checkQualityEntity, Model model,HttpServletRequest request,HttpServletResponse response) {
		try {
			//保存标准质检数据
			checkQualityService.saveData(checkQualityEntity);
			
		} catch (Exception e) {
			// 打印日志
			logger.error(e.getMessage(),e);
			// 返回信息
		    return renderString(response, AjaxDone.error(SystemMessageUtils.getMessageName("System_error_please_contact_your_administrator")));
		}
		// 返回信息
	    return renderString(response, AjaxDone.error(SystemMessageUtils.getMessageName("audit_ok_msg")));
	}
}
